//
//  ShipFSM.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-26.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "ShipFSM.h"
#import "Ship.h"

#pragma mark -
#pragma mark ShipSearch,搜敌状态机
@implementation ShipSearch
@synthesize manager = _manager;

+(id) searchStateWithManager:(ShipFSMManager*)manager
{
     return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入搜敌状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出搜敌状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipLockTarget,锁定状态机
@implementation ShipLockTarget
@synthesize manager = _manager;

+(id) lockTargetStateWithManager:(ShipFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入锁定状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出锁定状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipCloseTo,接近目标
@implementation ShipCloseTo
@synthesize manager = _manager;

+(id) closeToStateWithManager:(ShipFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入接近目标状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出接近目标状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipSpecialAttack,特攻状态机
@implementation ShipSpecialAttack
@synthesize manager = _manager;

+(id) specialAttackStateWithManager:(ShipFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入特攻状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出特攻状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipFire,开火状态机
@implementation ShipFire
@synthesize manager = _manager;

+(id) fireStateWithManager:(ShipFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入开火状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出开火状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipEvacuate,撤离状态机
@implementation ShipEvacuate
@synthesize manager = _manager;

+(id) evacuateStateWithManager:(ShipFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(ShipFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"船舰：进入撤离状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"船舰：退出撤离状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark ShipFSMManager,船舰状态机管理
@implementation ShipFSMManager
@synthesize ship = _ship;
@synthesize searchState = _searchState;
@synthesize lockTargetState = _lockTargetState;
@synthesize closeToState = _closeToState;
@synthesize specialAttackState = _specialAttackState;
@synthesize fireState = _fireState;
@synthesize evacuateState = _evacuateState;

+(id) managerWithShip:(Ship*)ship
{
     return [[[self alloc] initWithShip:ship] autorelease];
}

-(id) initWithShip:(Ship*)ship
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        //纪录船舰的引用
        self.ship = ship;
        
        //创建状态机
        self.searchState = [ShipSearch searchStateWithManager:self];
        self.lockTargetState = [ShipLockTarget lockTargetStateWithManager:self];
        self.closeToState = [ShipCloseTo closeToStateWithManager:self];
        self.specialAttackState = [ShipSpecialAttack specialAttackStateWithManager:self];
        self.fireState = [ShipFire fireStateWithManager:self];
        self.evacuateState = [ShipEvacuate evacuateStateWithManager:self];
        
        //默认状态为搜敌状态
        [self toSearch];
    }
    
    return self;
}

/**
 进入搜敌状态
 */
-(void) toSearch
{
    [super changeState:_searchState];
}

/**
	进入锁定目标状态
 */
-(void) toLockTarget
{
    [super changeState:_lockTargetState];
}


/**
	进入接近状态
 */
-(void) toCloseTo
{
    [super changeState:_closeToState];
}

/**
	进入特攻状态
 */
-(void) toSpecialAttack
{
    [super changeState:_specialAttackState];
}

/**
	进入开火状态
 */
-(void) toFire
{
    [super changeState:_fireState];
}

/**
	进入撤离状态
 */
-(void) toEvacuate
{
    [super changeState:_evacuateState];
}

@end